package com.example.demo.controller;

import com.example.demo.annotation.MyRequestBody;
import com.example.demo.config.FeatureConfig;
import com.example.demo.entity.Goods;
import com.example.demo.entity.Middle;
import com.example.demo.entity.User;
import com.example.demo.service.GoodsService;
import com.example.demo.service.MiddleService;
import com.example.demo.vo.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * @Description
 * @Author chenhuiyu
 * @Date 2023/12/1
 */
@RestController
@RequestMapping(value = "/goods")
public class GoodsController {
    private static final Logger log = LoggerFactory.getLogger(GoodsController.class);
    @Autowired
    private GoodsService goodsService;

    @Autowired
    private MiddleService middleService;

    @Autowired
    private FeatureConfig featureConfig;

    @PostMapping("/add")
    public Result addGoods(@RequestBody Goods goods) {
        Result result = new Result();
        if (goods.getGroupId() == null && goods.getUserId() == null){
            result.setMessage("用户或组信息有误");
            return result;
        }
        if (goods.getGroupId() == null){
            List<Middle> middleList = middleService.findMiddleListByUserId(goods.getUserId());
            if (!middleList.isEmpty()) {
                goods.setGroupId(middleList.get(0).getGroupId());
            } else {
                result.setMessage("用户信息存在问题");
                return result;
            }
        }
        Long l = goodsService.addGoods(goods);
        if (l > 0) {
            result.setMessage("添加成功");
            result.setObject(l);
        } else {
            result.setMessage("添加失败");
        }
        return result;
    }

    @PostMapping("/update")
    public Result updateGoods(@RequestBody User user) {
        Long l = goodsService.updateGoods(user);
        Result result = new Result();
        if (l > 0) {
            result.setMessage("修改成功");
            result.setObject(l);
        } else {
            result.setMessage("修改失败");
        }
        return result;
    }

    @PostMapping("/find/goodsList")
    public Result findGoodsList(@MyRequestBody Integer userId, @MyRequestBody String num){
        log.debug("其他信息{}", featureConfig.toString());
        Result result = new Result();
        User user = new User();
        System.out.println("num " + num);
        user.setId(userId);
        Map<String, Goods> map = goodsService.findGoods(user);
        result.setObject(map);
        return result;
    }

    @PostMapping("/find/goods")
    public Result findGoods(@MyRequestBody User user, @MyRequestBody Goods goods){
        log.debug("其他信息{}", featureConfig.toString());
//    public Result findGoods(@RequestBody MiddleVo middleVo){
//        User user = middleVo.getUser();
//        Goods goods = middleVo.getGoods();
        Result result = new Result();
        //
        List<Middle> middleList = middleService.findMiddleListByUserId(user.getId());
        boolean a = false;
        for (Middle m : middleList) {
            if (Objects.equals(m.getGroupId(), goods.getGroupId())) {
                a = true;
                break;
            }
        }
        // 或多线程下更好性能
        AtomicBoolean b = new AtomicBoolean(false);
        middleList.forEach(m -> {
            if (Objects.equals(m.getGroupId(), goods.getGroupId())){
                b.set(true);
            }
        });
//        System.err.println(a + " " + b);
//        log.debug(a + " " + b);
        List<Goods> goodsList1 = new ArrayList<>();
        List<Goods> goodsList2 = new ArrayList<>();
        if (b.get()){
            goodsList1 = goodsService.findGoodsListByGroupId(goods.getGroupId());
//            System.out.println(goodsList1);
        }
        System.err.println("------------------");
        //
        if (Objects.equals(user.getGroupId(), goods.getGroupId())){
//            System.err.println("user.groupId == goods.groupId");
            log.debug("user.groupId == goods.groupId");
            goodsList2 = goodsService.findGoodsListByGroupId(goods.getGroupId());
//            System.out.println(goodsList2);
        } else {
//            System.err.println("user.groupId != goods.groupId");
            log.debug("user.groupId != goods.groupId");
        }
//        System.err.println(Objects.equals(goodsList1, goodsList2));
        log.debug("goodsList1 == goodsList2 {}", Objects.equals(goodsList1, goodsList2));

        result.setMessage("success");
        result.setObject(Objects.equals(goodsList1, goodsList2));
        result.setObject(middleList);
        return result;
    }

    @PostMapping("/list")
    public Result findByIdList(@MyRequestBody List<Integer> goodsIdList){
        log.debug("其他信息{}", featureConfig.toString());
        List<Goods> goodsList = goodsService.findGoodsByIdList(goodsIdList);
        Result result = new Result();
        result.setMessage("success");
        result.setObject(goodsList);
        return result;
    }

}
